package zx.com.camratest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.github.faucamp.simplertmp.RtmpHandler;
import com.seu.magicfilter.utils.MagicFilterType;

import net.ossrs.yasea.SrsCameraView;
import net.ossrs.yasea.SrsEncodeHandler;
import net.ossrs.yasea.SrsPublisher;
import net.ossrs.yasea.SrsRecordHandler;

import java.io.IOException;
import java.net.SocketException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RtmpHandler.RtmpListener,
        SrsEncodeHandler.SrsEncodeListener, SrsRecordHandler.SrsRecordListener {
    private SrsPublisher mPublisher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        startLive();
    }

    private void initData() {
        //开启 创建房间和直播
        mPublisher = new SrsPublisher((SrsCameraView) findViewById(R.id.camera_surface));
        mPublisher.setEncodeHandler(new SrsEncodeHandler(this));
        mPublisher.setRtmpHandler(new RtmpHandler(this));
        mPublisher.setRecordHandler(new SrsRecordHandler(this));
        if (!mPublisher.setPreviewResolution(640, 480)) {
            Intent i = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(getBaseContext().getPackageName());
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            return;
        }
    }

    @Override
    protected void onDestroy() {
        stopLive();
        super.onDestroy();
    }

    //关闭直播
    private void stopLive() {
        mPublisher.stopPublish();
        mPublisher.stopEncode();
    }

    //开始直播
    private void startLive() {
        if (isFinishing()) return;
//        startAnimation(3, 1000);
        mPublisher.setOutputResolution(480, 640);
        mPublisher.setVideoHDMode();
        mPublisher.startPublish("rtmp://video-center.alivecdn.com/mid-starlive/807C5QN5-11492push?vhost=bobo.mid-cent.com");
        mPublisher.switchCameraFilter(MagicFilterType.BEAUTY);
    }

    public void gotoPlay(View v) {
        Intent i = new Intent(this, Main2Activity.class);
        startActivity(i);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onRtmpConnecting(String msg) {

    }

    @Override
    public void onRtmpConnected(String msg) {

    }

    @Override
    public void onRtmpVideoStreaming() {

    }

    @Override
    public void onRtmpAudioStreaming() {

    }

    @Override
    public void onRtmpStopped() {

    }

    @Override
    public void onRtmpDisconnected() {

    }

    @Override
    public void onRtmpVideoFpsChanged(double fps) {

    }

    @Override
    public void onRtmpVideoBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpAudioBitrateChanged(double bitrate) {

    }

    @Override
    public void onRtmpSocketException(SocketException e) {

    }

    @Override
    public void onRtmpReconnectException() {

    }

    @Override
    public void onRtmpIOException(IOException e) {

    }

    @Override
    public void onRtmpIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRtmpIllegalStateException(IllegalStateException e) {

    }

    @Override
    public void onNetworkWeak() {

    }

    @Override
    public void onNetworkResume() {

    }

    @Override
    public void onEncodeIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRecordPause() {

    }

    @Override
    public void onRecordResume() {

    }

    @Override
    public void onRecordStarted(String msg) {

    }

    @Override
    public void onRecordFinished(String msg) {

    }

    @Override
    public void onRecordIllegalArgumentException(IllegalArgumentException e) {

    }

    @Override
    public void onRecordIOException(IOException e) {

    }
}
